<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" :request-api="getAdvList">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button v-auth="'adv_save'" type="primary" :icon="CirclePlus" @click="openDrawer('新增')"> 新增广告 </el-button>
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button v-auth="'adv_save'" type="primary" link :icon="EditPen" @click="openDrawer('编辑', scope.row)">
          编辑
        </el-button>
        <el-button v-auth="'adv_del'" type="danger" link :icon="Delete" @click="deleteAdvPosition(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <AdvEdit ref="drawerRef" />
  </div>
</template>
<script setup lang="tsx" name="advPosition">
import { ref, reactive } from "vue";
import { Market } from "@/api/interface";
import ProTable from "@/components/ProTable/index.vue";
import AdvEdit from "@/views/market/adv/AdvEdit.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { CirclePlus, Delete, EditPen } from "@element-plus/icons-vue";
import { getAdvList, saveAdv, delAdv } from "@/api/modules/market";
import { useHandleData } from "@/hooks/useHandleData";
// ProTable 实例
const proTable = ref<ProTableInstance>();

// 表格配置项
const columns = reactive<ColumnProps<Market.Adv>[]>([
  { prop: "name", label: "广告名称", search: { el: "input" } },
  { prop: "positionCode", label: "广告位标识", search: { el: "input" } },
  { prop: "link", label: "跳转链接" },
  {
    prop: "contentShow",
    label: "内容",
    render: scope => {
      return <el-image src={scope.row.contentShow} style="width: 100px; height: 100px" />;
    }
  },
  { prop: "startTime", label: "开始时间" },
  { prop: "endTime", label: "结束时间" },
  { prop: "updatedAt", label: "操作时间" },
  { prop: "operation", label: "操作", width: 250, fixed: "right" }
]);

// 删除权限资源
const deleteAdvPosition = async (params: Market.Adv) => {
  await useHandleData(delAdv, { ids: params.id }, `删除【${params.name}】广告`);
  proTable.value?.getTableList();
};

// 打开 drawer(新增、查看、编辑)
const drawerRef = ref<InstanceType<typeof AdvEdit> | null>(null);
const openDrawer = (title: string, row: Partial<Market.Adv> = {}) => {
  const params = {
    title,
    isView: title === "查看",
    row: { ...row },
    api: saveAdv,
    getTableList: proTable.value?.getTableList
  };
  drawerRef.value?.acceptParams(params);
};
</script>
